package com.b2b.util;

import com.alibaba.fastjson.JSONObject;
import com.b2b.model.entity.goods.Stock;
import com.b2b.model.receive.BriefGood;
import com.b2b.model.receive.StockInfo;
import com.b2b.service.goodsService.StockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class StockUtil {
    private final StockService stockService;

    private final GoodsUtil goodsUtil;

    @Autowired
    public StockUtil(StockService stockService, GoodsUtil goodsUtil){
        this.goodsUtil = goodsUtil;
        this.stockService = stockService;
    }

    public StockInfo getStockInfo(Integer stockId){
        StockInfo stockInfo = new StockInfo();
        Stock stock = new Stock();
        stock.setStockId(stockId);
        stock = stockService.selectByPrimaryKey(stock);

        JSONObject modelMap = JSONObject.parseObject(stock.getModelMap());

        BriefGood briefGood = goodsUtil.getBriefGoodById(stock.getGoodsId());

        stockInfo.setStockId(stockId);
        stockInfo.setGoodsId(briefGood.getGoodsId());
        stockInfo.setGoodsName(briefGood.getGoodsName());
        stockInfo.setGoodsImgUrl(briefGood.getGoodsImgUrlList().get(0));
        stockInfo.setModelMap(modelMap);
        stockInfo.setPrice(stock.getPrice());
        stockInfo.setNumber(stock.getNumber());
        return stockInfo;
    }
}
